#install.packages("sm")


library(tidyverse)
library(overlap)
library(sm)
library(xtable)

setwd("C:/Users/stien/Documents/Facebook/replication material")
dat <- read_csv("data/density.csv")

dat$Politics <- lapply(strsplit(as.character(dat$Politics),"[][']|,\\s*"), function(x) as.numeric(x))
dat$Polnews <- lapply(strsplit(as.character(dat$Polnews),"[][']|,\\s*"), function(x) as.numeric(x))
dat$Hardnews <- lapply(strsplit(as.character(dat$Hardnews),"[][']|,\\s*"), function(x) as.numeric(x))
dat$Lifestyle <- lapply(strsplit(as.character(dat$Lifestyle),"[][']|,\\s*"), function(x) as.numeric(x))

df = NULL
for (i in list(2,3,4,5))
{
  
  lib = dat[[1,i]][[1]][-1]
  lib = lib[!is.na(lib)]
  mod = dat[[2,i]][[1]][-1]
  mod =mod[!is.na(mod)]
  con=dat[[3,i]][[1]][-1]
  con =con[!is.na(con)]
  lib=lib*2*pi
  mod=mod*2*pi
  con=con*2*pi
  bootx = bootstrap(lib, mod, 1000, type="Dhat4")
  x = mean(bootx)
  booty = bootstrap(lib, con,1000, type="Dhat4")
  y= mean(booty)
  bootz = bootstrap(mod, con, 1000,type="Dhat4")
  z=mean(bootz)
  df = rbind(df, data.frame(x,y,z))
}

xtable(df)


overlapEst(con, lib, type="Dhat4")
overlapEst(lib, mod, type="Dhat4")
overlapEst(con, mod, type="Dhat4")